import scrapy

from scrapy_splash import SplashRequest
from scrapy_redis.spiders import RedisCrawlSpider
from selenium import webdriver
from bilispider.items import BilispiderItem

# class VideosSpider(scrapy.Spider):
class VideosSpider(RedisCrawlSpider):

    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument('window-size=1920x1080')  # 虚拟屏幕大小
    browser = webdriver.Chrome(options=chrome_options)
    item = BilispiderItem()
    name = 'videos'
    # allowed_domains = ['www.baidu.com']
    # start_urls = ['https://www.bilibili.com/video/BV1rz411e71c?from=search&seid=9892985571931013336']
    redis_key = "urlq"  # url 队列名

    def parse(self, response):
        title = response.xpath('//h1[@class="video-title"]/span/text()').extract_first()
        time = response.xpath('//div[@class="video-data"]/span[2]/text()').extract_first()
        view = response.xpath('//span[@class="view"]/text()').extract_first().replace(" ","").replace("·",'').replace('\xa0','') # 播放量
        dm = response.xpath('//span[@class="dm"]/text()').extract_first() # 弹幕
        # 功能div
        div = response.xpath('//div[@class="ops"]')
        like = div.xpath('./span[1]//text()').extract_first().replace(" ","").replace("\n",'') # 点赞
        coin = div.xpath('./span[2]//text()').extract_first().replace(" ","").replace("\n",'') # 投币
        collect = div.xpath('./span[3]//text()').extract_first().replace(" ","").replace("\n",'') # 收藏
        tag_list=response.xpath('//li[@class="tag"]/a//text()').extract() # 标签
        info = response.xpath('//*[@id="v_desc"]/div/text()').extract_first()  # 详情
        self.item['title']=title
        self.item['time']=time
        self.item['view']=view
        self.item['dm']=dm
        self.item['info']=info
        self.item['like']=like
        self.item['coin']=coin
        self.item['collect']=collect
        self.item['tag_list']=tag_list

        yield self.item


